package com.bofa.manager.test.cases;

import static org.testng.Assert.assertEquals;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.bofa.dao.TestDataDao;
import com.bofa.dao.TestDataExcelDaoImpl;
import com.bofa.manager.domain.Account;
import com.bofa.test.cases.base.BOFASeleniumBase;
import com.bofa.test.cases.enums.Selector;


public class AddNewAccount extends BOFASeleniumBase {
	static 	Logger logger  = Logger.getLogger(AddNewAccount.class.getName());
	TestDataDao testDataDao = new TestDataExcelDaoImpl();
	
	private List <Account> addNewAccountDataList = new ArrayList<Account>();
	private List <Account> validAddNewAccountDataList = new ArrayList<Account>();
	private List <Account> invalidAddNewAccountDataList = new ArrayList<Account>(); 

@BeforeClass
public void initialiseSeleniumDrivers() throws Exception {
	logger.info("Reading Data from Excel ..");
	addNewAccountDataList = testDataDao.geAddNewAccountData();
	for (Account account : addNewAccountDataList) {
		if("VALID".equals(account.getCategory())) {
			validAddNewAccountDataList.add(account);
		}else if("INVALID".equals(account.getCategory())){
			invalidAddNewAccountDataList.add(account);
		}
	}
}

@DataProvider(name ="validAccountData")
public  Object [][]  validAccount(){
	int validAccountDataSize = validAddNewAccountDataList.size();
	Object [][] validAccountDatas = new Object [validAccountDataSize][1];
	int i=0;
	for (Account objects : validAddNewAccountDataList) {
		validAccountDatas [i][0] =objects;
		i =i+1;
	}
	
	
	return validAccountDatas;
}
@DataProvider(name="invalidAccountData")
public Object[][] invalidAccount(){
	int invalidAccountDataSize = invalidAddNewAccountDataList.size();
	Object [][] invalidAccountDatas = new Object [invalidAccountDataSize][1];
	int i =0;
	for (Account objects :invalidAddNewAccountDataList ) {
		invalidAccountDatas[i][0] = objects;
		
	}
	
	return invalidAccountDatas;
	
}

@Test(enabled = true,priority =2,dataProvider="validAccountData",groups = "addNewAccounts" )
public void testWithValidAddAccountsData(Account account) throws InterruptedException {
	logger.info("TestingwithValidAccount " +account);
	boolean isLoginSucessfull = login();
	if(!isLoginSucessfull){
		assertEquals(true,false);
	}else {
		boolean isAccountCreated = addNewAccount(account);
		assertEquals(true,isAccountCreated);
		account.setSucess(isAccountCreated);
	}
}
@Test(enabled=true,priority=1,dataProvider="invalidAccountData",groups="addNewAccounts")
public void testWithInvalidAddAccountsData(Account account) throws InterruptedException{
	logger.info("TestingwithInValidAccount " + account);
	boolean isLoginSucessfull = login();
	if(!isLoginSucessfull){
		assertEquals(true,false);
	}else{
		boolean isAccountCreated = addNewAccount(account);
		assertEquals(false, isAccountCreated);
		account.setSucess(isAccountCreated);
	}
}
//add a new account
	private boolean addNewAccount(Account account) throws InterruptedException{
		boolean isAccountCreated = false;
		click(uiProperties.getProperty("bofa.page.new.account.link.text"), Selector.LINKTEXT);
		sendKeys(uiProperties.getProperty("bofa.page.new.account.customerId"), account.getCustomerId(), Selector.NAME);
		selctFrmDropdown(uiProperties.getProperty("bofa.page.new.account.select.accountType"), Selector.NAME, account.getAccountType());
	    sendKeys(uiProperties.getProperty("bofa.page.new.account.initialDeposit"), account.getInitialDeposit(), Selector.NAME);
	    click(uiProperties.getProperty("bofa.page.new.account.submit"),Selector.NAME );
	    Thread.sleep(3000);
	    if (isAlertPresent()) {
			String alertMessage = getAlerText();
			logger.info("Alert Message "+alertMessage);
	    }else {
	    String accountId = getText(uiProperties.getProperty("bofa.page.new.account.accountId"), Selector.XPATH);
	    logger.info("Account Number is " + accountId);
	    
	    if(account.getCustomerId().equals(getText(uiProperties.getProperty("bofa.page.new.account.get.customerId"),Selector.XPATH))) {
	    	logger.info("Customer Id is corect");
	    	if(account.getUsername().equals(getText(uiProperties.getProperty("bofa.page.new.account.username"),Selector.XPATH))) {
	    		logger.info("Username is correct");
	    		logger.info("Account creation successfull");
	    		isAccountCreated = true; 
	    	}
	    	account.setAccountId(accountId);
	     }
	    click(uiProperties.getProperty("bofa.page.new.account.btn.continue"), Selector.LINKTEXT);
	    }
	    return isAccountCreated;
	  }
@AfterGroups(groups= {"addNewAccounts"})
public void afterAllCreateNewAccountsTestCases()throws ParseException, IOException{
	for (Account account : validAddNewAccountDataList ) {
		logger.error("After tests "  + account);
	}
	for (Account account :invalidAddNewAccountDataList ) {
		logger.error("After tests " + account);
	}
    testDataDao.saveAddNewAccountsTestResults(addNewAccountDataList, getBrowser());	
}

}
